package com.jabex.manager.actions.product;

import com.jabex.common.exceptions.ErrorFunctionalException;
import com.jabex.common.exceptions.ErrorOperationalException;
import com.jabex.common.exceptions.WarningFunctionalException;
import com.jabex.common.to.MessageTO;
import com.jabex.common.to.ProductTO;
import com.jabex.common.util.Constants;
import com.jabex.core.bussines.IProductBp;
import com.jabex.manager.util.ConstantsManager;
import org.apache.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by jaugusto on 15/09/14. *
 */
@Controller
public class ProductController extends com.jabex.manager.actions.Controller {

	private ProductTO productTO;
	private List<ProductTO> productTOList;

	@Autowired
	@Qualifier(IProductBp.SERVICE_NAME)
	private IProductBp productBp = null;

	public ProductController() {
		this.productTO = new ProductTO();
	}

	/**
	 * Metodo para guardar un nuevo producto.
	 *
	 * @return resultado de la operacion (success/error)
	 */
	public String saveProduct() {
		String result = ConstantsManager.ERROR;
		try {
			MessageTO messageTO = productBp.insertProduct(productTO);
			if (messageTO.getCode().equals(Constants.OPERATION_OK)) {
				result = ConstantsManager.SUCCESS;
			}
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return result;
	}

	/**
	 * Metodo para eliminar un producto.
	 *
	 * @return resultado de la operacion (success/error)
	 */
	public String deleteProduct() {
		int id = 0;
		String result = ConstantsManager.ERROR;
		try {
			MessageTO messageTO = productBp.deleteProduct(id);
			if (messageTO.getCode().equals(Constants.OPERATION_OK)) {
				result = ConstantsManager.SUCCESS;
			}
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return result;
	}

	/**
	 * Metodo para actualizar los datos de un producto.
	 *
	 * @return resultado de la operacion (success/error)
	 */
	public String updateProduct() {
		ProductTO roductTo = new ProductTO();
		String result = ConstantsManager.ERROR;
		try {
			MessageTO messageTO = productBp.updateProduct(productTO);
			if (messageTO.getCode().equals(Constants.OPERATION_OK)) {
				result = ConstantsManager.SUCCESS;
			}
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return result;

	}


	/**
	 * Get all Product
	 *
	 * @return list of Products
	 */
	public List<ProductTO> getAllProduct() {
		try {
			productTOList = productBp.getAllProduct();
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return productTOList;
	}


	public ProductTO getProductTO() {
		return productTO;
	}

	public void setProductTO(ProductTO productTO) {
		this.productTO = productTO;
	}

	public List<ProductTO> getProductTOList() {
		return productTOList;
	}

	public void setProductTOList(List<ProductTO> productTOList) {
		this.productTOList = productTOList;
	}

	public IProductBp getProductBp() {
		return productBp;
	}

	public void setProductBp(IProductBp productBp) {
		this.productBp = productBp;
	}
}
